﻿Imports System.Collections.Generic
Imports System.Collections

Public Class InitializeData


    Public Class TVWS

        Public id As Integer
        Public subid As Integer
        Public bandwidth As Integer
        Public transmissionPower As Integer
        Public isAvailable As Boolean

        Public Sub New(ByVal id As Integer, ByVal subid As Integer, ByVal bandwidth As Integer, ByVal transmissionPower As Integer, ByVal isAvailable As Boolean)
            Me.bandwidth = bandwidth
            Me.id = id
            Me.subid = subid
            Me.transmissionPower = transmissionPower
            Me.isAvailable = isAvailable
        End Sub


    End Class

    Public Class SecondarySystem

        Public name As String
        Public id As Integer
        Public bandwidth As Integer
        Public transmissionPower As Integer
        Public priority As Integer
        Public start As Integer
        Public EndTime As Integer
        Public diff As Integer = 0
        Public potentialSolutions As New List(Of TVWS())

        Public Sub New(ByVal name As String, ByVal id As Integer, ByVal bandwidth As Integer, ByVal transmissionPower As Integer, ByVal priority As Integer, ByVal start As Integer, ByVal EndTime As Integer)
            Me.name = name
            Me.id = id
            Me.bandwidth = bandwidth
            Me.transmissionPower = transmissionPower
            Me.priority = priority
            Me.start = start
            Me.EndTime = EndTime
            Me.potentialSolutions = Calculations.getValidChannelsCombinations(Main.tvws, CInt(Main.bandwidth))

        End Sub

    End Class

    Public Class Calculations

        Public Shared Function getValidChannelsCombinations(ByVal channels As List(Of TVWS), ByVal bandwidth As Integer) As List(Of TVWS())
            Dim potentialSolutions As New List(Of TVWS())
            Dim validChannels(bandwidth) As TVWS
            'Dim validChannels As New ArrayList(bandwidth+1)
            Dim index As Integer
            For index = 0 To channels.Count - 1
                ReDim validChannels(0)
                ReDim validChannels(bandwidth)
                Dim currentChannel As TVWS = channels.Item(index)
                If (currentChannel.isAvailable) Then
                    Dim indexInSolution As Integer = 0
                    validChannels(indexInSolution) = currentChannel
                    indexInSolution = indexInSolution + 1
                    Dim maxIndexForSystem As Integer = index + (CInt(Main.bandwidth) + 1)
                    Dim index2 As Integer = index + 1
                    While (index2 < maxIndexForSystem And index2 < channels.Count)


                        'For index2 As Integer = index + 1 To (index2 < maxIndexForSystem And index2 < channels.Count)
                        currentChannel = channels.Item(index2)
                        If (currentChannel.isAvailable) Then
                            validChannels(indexInSolution) = currentChannel
                            indexInSolution = indexInSolution + 1
                        Else
                            Exit While
                        End If
                        index2 = index2 + 1
                    End While

                End If
                Dim hasEmpty As Boolean = False
                Dim t As TVWS
                For Each t In validChannels
                    If t Is Nothing Then
                        hasEmpty = True
                        Exit For
                    End If
                Next t
                If Not hasEmpty Then

                    potentialSolutions.Add(validChannels)
        
                End If

            Next index
            Return potentialSolutions
        End Function

    
    End Class

End Class
